//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Workflow.ComponentModel.Design;
using System.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Data.Odbc;
using System.Xml;
using Microsoft.Research.ScientificWorkflow;
using System.IO;
using System.Threading;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("OLAP Cube Visualizer")]
    [Description("Activity to visualize an OLAP cube.")]
    [WorkflowCategory("Database")]
    class OLAPVisualizer : Activity
    {
        #region Dependency Properties

        public static DependencyProperty OLAPServerNameProperty = DependencyProperty.Register("OLAPServerName",
            typeof(string), typeof(OLAPVisualizer));

        [RequiredInputParam]
        [Name("OLAP Server Name")]
        [Description("Name of the OLAP server.")]
        public string OLAPServerName
        {
            get
            {
                return ((string)(base.GetValue(OLAPVisualizer.OLAPServerNameProperty)));
            }
            set
            {
                base.SetValue(OLAPVisualizer.OLAPServerNameProperty, value);
            }
        }

        public static DependencyProperty CatalogNameProperty = DependencyProperty.Register("CatalogName",
            typeof(string), typeof(OLAPVisualizer));

        [RequiredInputParam]
        [Name("OLAP Catalog Name")]
        [Description("Name of the OLAP catalog or instance.")]
        public string CatalogName
        {
            get
            {
                return ((string)(base.GetValue(OLAPVisualizer.CatalogNameProperty)));
            }
            set
            {
                base.SetValue(OLAPVisualizer.CatalogNameProperty, value);
            }
        }

        #endregion

        #region Overridden methods

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Thread spWindow = new Thread(new ThreadStart(ThreadStartingPoint));
            spWindow.SetApartmentState(ApartmentState.STA);
            spWindow.IsBackground = true;
            spWindow.Start();

            return ActivityExecutionStatus.Closed;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Thread Proc which creates an instance of the window and adds
        /// the child controls.
        /// </summary>
        private void ThreadStartingPoint()
        {
            ScientificWorkflow.Forms.OLAPVisualizer vis = new ScientificWorkflow.Forms.OLAPVisualizer();
            vis.SetConnectionString(this.OLAPServerName, this.CatalogName);
            vis.Show();
            System.Windows.Threading.Dispatcher.Run();
        }

        #endregion
    }
}
